using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using NLog;

namespace GameShow.DAL
{
    public class Player03DAL : DALBase
    {
        public static Logger log = LogManager.GetCurrentClassLogger();
        public string GetAnsTrue(int iQuestionID)
        {
            string strAnsTrue = string.Empty;
            DataSet ds = new DataSet("TT");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SELECT ta.AnsSign FROM tblAnswers ta WHERE ta.QuestionId = "+iQuestionID +" AND ta.[Value] =1";
                command.Connection = AppConnection;
                adapter.SelectCommand = command;
                adapter.Fill(ds, "ITC");
                strAnsTrue = ds.Tables[0].Rows[0][0].ToString();
            }
            catch (Exception ex)
            {
                log.ErrorException(string.Empty, ex);
            }
            finally
            {
                AppConnection.Close();
            }
            return strAnsTrue;
        }

        public DataSet GetNameAndPosTeam(int iTeamId)
        {
            DataSet ds = new DataSet("TT");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SELECT * FROM tblTeams WHERE TeamId = " + iTeamId;
                command.Connection = AppConnection;
                adapter.SelectCommand = command;
                adapter.Fill(ds, "ITC");

            }
            catch (Exception ex)
            {
                log.ErrorException(string.Empty, ex);
            }
            finally
            {
                AppConnection.Close();
            }
            return ds;
        }

        public string GetNameAns(int iAnsId)
        {
            string strNameAns = string.Empty;
            DataSet ds = new DataSet("TT");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SELECT AnsSign FROM tblAnswers WHERE AnsId =" + iAnsId;
                command.Connection = AppConnection;
                adapter.SelectCommand = command;
                adapter.Fill(ds, "ITC");
                if (ds.Tables[0].Rows.Count>0)
                {
                    strNameAns = ds.Tables[0].Rows[0][0].ToString();
                }
                
            }
            catch (Exception ex)
            {
                log.ErrorException(string.Empty, ex);
            }
            finally
            {
                AppConnection.Close();
            }
            return strNameAns;
        }

        public string GetNameTeam(int iTeam)
        {
            string strNameTeam = string.Empty;
            DataSet ds = new DataSet("TT");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SELECT TeamName FROM tblTeams WHERE TeamId = "+ iTeam;
                command.Connection = AppConnection;
                adapter.SelectCommand = command;
                adapter.Fill(ds, "ITC");
                strNameTeam = ds.Tables[0].Rows[0][0].ToString();
            }
            catch (Exception ex)
            {
                log.ErrorException(string.Empty, ex);
            }
            finally
            {
                AppConnection.Close();
            }
            return strNameTeam;
        }

        public DataSet GetResultByQuestionId(int iQuestionId)
        {
            DataSet ds = new DataSet("TT");
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "SELECT * FROM tblResult WHERE QuestionId = " + iQuestionId ;
                command.Connection = AppConnection;
                adapter.SelectCommand = command;
                adapter.Fill(ds, "ITC");

            }
            catch (Exception ex)
            {
                log.ErrorException(string.Empty, ex);
            }
            finally
            {
                AppConnection.Close();
            }
            return ds;
        }

    }
}
